<template>
    <div class="login_container">
        <div class="login_box">
            <div class="avater-box">
                <img src="../../assets/user.gif" alt="">
            </div>

            <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginRef">

                <el-row>
                    <el-col :span="18">
                        <el-form-item prop="username">
                            <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"
                                      placeholder="请输入用户名" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="18">
                        <el-form-item prop="password">
                            <el-input v-model="loginForm.password" prefix-icon="iconfont icon-3702mima"
                                      placeholder="请输入密码"
                                      type="password" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-form-item class="butns">
                    <el-button type="primary" @click="submitForm('loginRef')">登录</el-button>
                    <el-button type="info" @click="formReset('loginRef')" style="margin-left:40px">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data() {
            return {
                //属性绑定
                loginForm: {
                    username: 'admin',
                    password: '123456'
                },
                //数据校验
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        //向后台发送请求对比数据
                        this.axios({
                            method: 'POST',
                            url: 'login',
                            data: this.loginForm
                        }).then((res) => {
                            //获取客户端返回的数据进行判断
                            if (res.status == '200') {
                                this.$message.success(res.data.meta.msg);
                                let token = res.data.data.token;
                                //将token放在本地存储起来
                                window.sessionStorage.setItem("token", token);
                                //跳转到首页
                                this.$router.push("/home");
                            }
                        });
                    } else {
                        this.$message.error("用户名或者密码错误")
                        return false;
                    }
                });
            },
            //重置表单信息
            formReset(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style lang="less" scoped>

    .el-row {
        margin-left: 120px
    }

    .el-col {
        border-radius: 4px;
        margin-top: 10px
    }

    .login_container {
        /*background-color: #2b4b6b;*/
        background: url(../../assets/images/bg.png) no-repeat center center fixed;
        height: 100%;
        -webkit-background-size: cover;
        -moz-background-size: cover;
        -o-background-size: cover;
        background-size: cover; //这条一定要
    }

    .login_form {
        position: absolute;
        bottom: 60px;
        width: 100%;
        padding: 0 20px;
        box-sizing: border-box;
        margin-top: 30px;
    }

    .login_box {
        width: 600px;
        height: 400px;
        /*background-color: #00000090;*/
        background-image: linear-gradient(to top, #9890e3 0%, #b1f4cf 100%);
        border-radius: 20px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
    }

    .butns {
        display: flex;
        justify-content: space-around;
    }

    .avater-box {
        height: 130px;
        width: 130px;
        border: 1px solid #eeee;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px #ddd;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: #fff;

        img {
            height: 100%;
            width: 100%;
            border-radius: 50%;
            background-color: #eee;
        }
    }
</style>
